﻿@model IEnumerable<FCNS.ERP.Models.ErpOrderFoodIO>

@{
    ViewBag.Title = "点餐";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="~/Content/Food/food.css" rel="stylesheet" />
<script src="~/Scripts/angular.min.js"></script>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function ($scope) {
        $scope.foods = new Array();

        function HideUpdate() {
            if ($scope.foods.length > 0) {
                $(".OrderFoodIoTable").fadeIn(0);
            }
            else {
                $(".OrderFoodIoTable").fadeOut(0);
            }
        }

        HideUpdate();
        //加载所有菜品信息
        var json = angular.fromJson(@Html.Raw(ViewData["ProductJson"]));

        $scope.addFood = function () {
            /*
            菜品编码.数量.菜品编码.数量.菜品编码.数量
            普通菜品可以连续录入
            组合菜品这样录入则第一个为组合菜品，后续为组合菜品的子项目
            */
            var userInput = $scope.newFood.split('.');
            if (userInput.length < 2) {
                return;
            }
            //读取第一个确定是否 组合菜品
            var firstFood = null;
            for (var v = 0; v < json.length; v++) {
                if (json[v].Id == userInput[0] && $.isNumeric(userInput[1])) {
                    firstFood = json[v];
                    break;
                }
            }
            //计算商品
            if (firstFood == null) {
                return;
            }

            if (firstFood.Group != "null") {
                var temp = new Array();
                for (var i = 2; i < userInput.length; i += 2) {
                    for (var v = 0; v < json.length; v++) {
                        if (json[v].Id == userInput[i] && $.isNumeric(userInput[i + 1])) {
                            var tempJ = json[v];
                            temp.unshift([tempJ.Id, tempJ.Name, userInput[i + 1]]);
                            break;
                        }
                    }
                }
               firstFood.Group = temp;
                    $scope.foods.unshift(firstFood);
                    $scope.newFood = "";
            }
            else {
                for (var i = 0; i < userInput.length; i += 2) {
                    var canAddFood = null;
                    for (var v = 0; v < json.length; v++) {
                        if (json[v].Id == userInput[i] && $.isNumeric(userInput[i + 1])) {
                            canAddFood = json[v];
                            canAddFood.Temp = userInput[i + 1];
                            break;
                        }
                    }
                    if (canAddFood != null) {
                        $scope.foods.unshift(canAddFood);
                        $scope.newFood = "";
                    }
                }
            }

            HideUpdate();
        }

        $scope.removeFood = function (Id) {
            for (var v = 0; v < $scope.foods.length; v++) {
                if ($scope.foods[v].Id == Id) {
                    $scope.foods.splice(v, 1);
                    break;
                }
            }
            HideUpdate();
        }

        $scope.updateFood = function () {
            var v = "";
            for (var i = 0; i < $scope.foods.length; i++)
            {
                var temp = $scope.foods[i];
                v += (temp.Id + "." + temp.Temp );
                if (temp.Group != "null")
                {
                    v += "(";
                    for (var j = 0; j < temp.Group.length; j++)
                    {
                        var temp2 = temp.Group[j];
                       v += (temp2[0] + "," + temp2[2] + "*");
                    }
                    v += ")";
                }
                v += ";";
            }
           document.location.replace("/Food/OrderFoodIoInsert?seatId=" +@ViewData["SeatId"]+"&food=" + v);
        }

        $scope.cancelFood = function (Id) {
     document.location.replace("/Food/OrderFoodIoCancel?seatId=" +@ViewData["SeatId"]+"&ioId=" +Id);
        }
    });
</script>

<div ng-app="myApp" ng-controller="myCtrl">
 <input type="hidden" id="pJson" value=@Html.Raw(ViewData["ProductJson"]) />

<form class="form-horizontal" >
    <div class="row">
        <label class="control-label col-md-2" for="AddFood">请输入点餐信息：</label>
        <div class="col-md-2">
            <input id="food" class="form-control" type="text" ng-model="newFood" />
        </div>
        <button type="button" class="btn btn-default" ng-click="addFood()">添加</button>
    </div>
  <table class="OrderFoodIoTable">
      <tr><th>菜品</th><th>数量</th><th></th></tr>
      <tr ng-repeat="x in foods"><td>{{x.Name}}<br /><ul><li ng-repeat="y in x.Group">    {{y[1]}} * {{y[2]}}</li></ul></td><td>{{x.Temp}}</td><td><img src="~/Content/Food/delete_24px.png" ng-click="removeFood(x.Id)" /></td></tr>
  <tr><td colspan="3"><button type="button" class="btn btn-default" ng-click="updateFood()">提交</button></td></tr>
  </table>
</form>

    <table class="table" id="foodTableList"  >
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ProductId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Count)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Price)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Status)
            </th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Product.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Count)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Status)
                    @if(item.Status== FCNS.ERP.Models.ErpOrderFoodIO.EnumOrderFoodIOstatus.下单 && (string.IsNullOrEmpty(item.GroupFlag)||item.IsGroup))
                    {
                        <label   ng-click="cancelFood(@item.Id)"> | 取消</label>
                    }
                </td>
            </tr>
        }

    </table>
    </div>